package clientP;

import conex.ClientInt;
import conex.ServerInt;
import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
import java.util.Random;
import Figuras.*;
import java.awt.event.KeyEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 *
 * @author Sandro
 */
public class Client extends LienzoKL implements ClientInt{//Runnable
    transient Vector vectorMapa;
    transient Vector vectorFiguras;
    static private ServerInt stubServer;
    private MenuClient menu;


    public static void main(String[] args) {
        new Client();
    }

    public Client(){
        super("El juego Vidas", 700, 600);
        this.menu = new MenuClient(this);
        menu.setUserName("Jugador"+(new Random()).nextInt(100));
        this.menu.setIp(getHostIp());// setea el ip localhost por defecto
  
   }

   protected void registrarEnServer(){

        try {
            UnicastRemoteObject.exportObject(this, 0);
            Registry registry = LocateRegistry.getRegistry(menu.getIp());
            stubServer = (ServerInt) registry.lookup(menu.getGameName());
            stubServer.register(this);
            menu.setSatus("Conectado");
            menu.setStartGameEnabled(false);
        } catch (Exception e) {
            try {
                UnicastRemoteObject.unexportObject(this, true);
            } catch (NoSuchObjectException ex) {
                System.err.println("Client exception: " + ex.toString());
            }
            menu.setSatus("No se pudo establecer conexión!");
            menu.setStartGameEnabled(true);
            System.err.println("Client exception: " + e.toString());
            e.printStackTrace();
        }
   }

     private String getHostIp(){
        InetAddress addr=null;
        try {
            addr = InetAddress.getLocalHost();
         } catch (UnknownHostException ex) {
            System.err.println("Client exception: " + ex.toString());
            ex.printStackTrace();
        }
       return addr.getHostAddress();
    }

    public void recibirMapa(Object mapa) throws RemoteException {
        this.setF((Vector<Figura>)mapa);
        this.repaint();
    }
    transient int i;
    public void update(Object arg) throws RemoteException {
        this.setF2((Vector<Figura>) arg);
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        String tecla = tc.get(e.getKeyCode()).toString();
        switch(tecla.charAt(0)){
            case '0':
                this.mandarAccion("4");
                break;
            case '1':
                this.mandarAccion("5");
                break;
            case '2':
                this.mandarAccion("6");
                break;
            case '3':
                this.mandarAccion("7");
                break;
            case '5':
                this.mandarAccion("9");
                break;
            default:
                break;
        }
    }
@Override
    public void keyPressed(KeyEvent e) {
        String tecla = tc.get(e.getKeyCode()).toString();
        switch(tecla.charAt(0)){
            case '0':
                this.mandarAccion("0");
                break;
            case '1':
                this.mandarAccion("1");
                break;
            case '2':
                this.mandarAccion("2");
                break;
            case '3':
                this.mandarAccion("3");
                break;
            case '5':
                this.mandarAccion("8");
                break;
            default:
                break;
        }
    }
   
    public void mandarAccion(String s){
        try {
            stubServer.accion(this, s);
        } catch (RemoteException ex) {
                 System.out.printf("\n No conection");
        }
    }
    @Override
    public String toString(){
        return "Cliente:"+this.getUserName();
    }
}
